package ro.uaic.info.fmse.context.transformers.tests;

import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.Test;

import ro.uaic.info.fmse.context.transformers.context.ContextTransformer;
import ro.uaic.info.fmse.context.transformers.context.Mapping;
import ro.uaic.info.fmse.context.transformers.enums.KType;
import ro.uaic.info.fmse.context.transformers.enums.Multiplicity;
import ro.uaic.info.fmse.context.transformers.enums.Open;
import ro.uaic.info.fmse.context.transformers.generic.Node;
import ro.uaic.info.fmse.context.transformers.generic.Tree;
import ro.uaic.info.fmse.context.transformers.items.ConfigurationNode;
import ro.uaic.info.fmse.context.transformers.items.RuleNode;

public class ContextTransformerTest {

	@Test
	public void testGetMappings() throws CloneNotSupportedException {

		// create 2 trees
		// create configuration
		Node<ConfigurationNode> T = new Node<ConfigurationNode>(10, "T",
				KType.BAG, new ConfigurationNode(Multiplicity.NONE, Multiplicity.NONE), "");
		Node<ConfigurationNode> thread = new Node<ConfigurationNode>(20,
				"client", KType.BAG, new ConfigurationNode(Multiplicity.ANY, Multiplicity.ANY), "");
		Node<ConfigurationNode> trt = new Node<ConfigurationNode>(30, "server",
				KType.BAG, new ConfigurationNode(Multiplicity.NONE, Multiplicity.NONE), "");
		Node<ConfigurationNode> k1 = new Node<ConfigurationNode>(40, "k",
				KType.K, new ConfigurationNode(Multiplicity.NONE, Multiplicity.ANY), "");
		Node<ConfigurationNode> k2 = new Node<ConfigurationNode>(50, "k",
				KType.K, new ConfigurationNode(Multiplicity.ANY, Multiplicity.ANY), "");
		
		
		Tree<ConfigurationNode> configuration = new Tree<ConfigurationNode>(T);
		configuration.appendNode(thread, T);
		configuration.appendNode(trt, T);
		configuration.appendNode(k1, thread);
		configuration.appendNode(k2, trt);
		
		System.out.println("Configuration: \n" + configuration + "\n\n");

		// create rule tree
		Node<RuleNode> TR = new Node<RuleNode>(100, "T", KType.BAG,
				new RuleNode(Open.NONE, 1), "");
		Node<RuleNode> k1R = new Node<RuleNode>(200, "k", KType.K,
				new RuleNode(Open.NONE, 2), "");
		Node<RuleNode> k2R = new Node<RuleNode>(300, "k", KType.K,
				new RuleNode(Open.NONE, 2), "");
		
		
		Tree<RuleNode> rule = new Tree<RuleNode>(TR);
		rule.appendNode(k1R, TR);
		rule.appendNode(k2R, TR);

		System.out.println("Rule: \n" + rule + "\n\n");
		
		
		ContextTransformer ct = new ContextTransformer();
		List<Mapping> mappings = ct.getMappings(configuration, rule);
		System.out.println("Witnesses: \n" + mappings + "\n\n");

		
		assertTrue(mappings != null);
		List<Tree<ConfigurationNode>> trees = ct.getTrees(mappings, configuration, rule);
		
		
		System.out.println("All possibilities:");
		assertTrue(trees != null);
		for (Tree<ConfigurationNode> t:trees)
			System.out.println(t + "\n\n\n");	
		 
	
		System.out.println("Context (filters applied):\n");
		trees = ct.getContext(configuration, rule);
		
		assertTrue(trees != null);
		for (Tree<ConfigurationNode> t:trees)
			System.out.println(t + "\n\n\n");	
	}
}
